﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Collections.Generic;

using PowerCreator.LMS.Common.Script;
using PowerCreator.LMS.Common.Utils;
using PowerCreator.LMS.DL.Project.Entity;
using PowerCreator.LMS.Web.DL.Project;
using PowerCreator.LMS.Web.Catalog;
using PowerCreator.LMS.Web.User;
using PowerCreator.LMS.User.Entity;


public partial class  sysadmin_DL_Project_AddClassUser:PowerCreator.LMS.Web.Base.WebBasePage
{

    VOTOSchoolGroup voSchool = new VOTOSchoolGroup();
    TOSchoolGroup objSchool = new TOSchoolGroup();
    VOTTeacherJob voJob = new VOTTeacherJob();
    Job objJob = new Job();
    VOTTeacherDuty voDuty = new VOTTeacherDuty();
    Duty objDuty = new Duty();
    private int thisPageSize = 24;
    PowerCreator.LMS.ViewQuery.ViewQuery objView = new PowerCreator.LMS.ViewQuery.ViewQuery();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ViewState["SearchType"] = "0";  //0简单搜索1高级搜索

            InitPage();

            int CID = 0;
            if (Request.QueryString["CID"] != null)
            {
                CID = int.Parse(Request.QueryString["CID"].ToString());
                ViewState["CID"] = CID.ToString();
            }

            //BinderUserGroup();
            BinderRegion();
            BinderSchool();
            BinderSchoolStage();
            BinderCatalog();
            BinderXueNian();
            BinderJob();
            BinderDuty();
            BinderXueLi();
            BinderSepcial();

            string search = GetSearch();
            BinderPager(search);
            BinderRPT(search, 1);
        }
    }




        //绑定用户组
    //private void BinderUserGroup()
    //{
    //    TOUserGroup objUserGroup = new TOUserGroup();
    //    string search = "";
    //    DataTable dt = objUserGroup.Query(search);
    //    ddl_UserGroup.DataSource = dt;
    //    ddl_UserGroup.DataTextField = "Name";
    //    ddl_UserGroup.DataValueField = "UGroup_Id";
    //    ddl_UserGroup.DataBind();

    //    ListItem li = new ListItem();
    //    li.Text = "全部";
    //    li.Value = "0";
    //    ddl_UserGroup.Items.Insert(0, li);
    //}

    //绑定区域
    private void BinderRegion()
    {
        TORegion obj = new TORegion();
        string search = "";
        DataTable dt = obj.Query(search);
        ddl_region.DataSource = dt;
        ddl_region.DataTextField = "Region";
        ddl_region.DataValueField = "ID";
        ddl_region.DataBind();

        ListItem li = new ListItem();
        li.Text = "全部";
        li.Value = "0";
        ddl_region.Items.Insert(0, li);
    }

    //绑定学校
    private void BinderSchool()
    {
        TOSchoolGroup obj = new TOSchoolGroup();
        string search = "";
        DataTable dt = obj.Query(search);
        ddl_School.DataSource = dt;
        ddl_School.DataTextField = "SchoolGroupName";
        ddl_School.DataValueField = "SchoolGroup_Id";
        ddl_School.DataBind();

        ListItem li = new ListItem();
        li.Text = "全部";
        li.Value = "0";
        ddl_School.Items.Insert(0, li);
    }


    //绑定学段
    private void BinderSchoolStage()
    {
        TOSchoolStage obj = new TOSchoolStage();
        string search = "";
        DataTable dt = obj.Query(search);
        ddl_xueduan_Stage.DataSource = dt;
        ddl_xueduan_Stage.DataTextField = "SchoolStage";
        ddl_xueduan_Stage.DataValueField = "ID";
        ddl_xueduan_Stage.DataBind();

        ListItem li = new ListItem();
        li.Text = "全部";
        li.Value = "0";
        ddl_xueduan_Stage.Items.Insert(0, li);
    }

    //绑定学科
    private void BinderCatalog()
    {
        Catalog obj = new Catalog();
        string search = "";
        DataTable dt = obj.QueryCatalog(search);
        ddl_xeke_Catalog.DataSource = dt;
        ddl_xeke_Catalog.DataTextField = "CatalogName";
        ddl_xeke_Catalog.DataValueField = "CatalogId";
        ddl_xeke_Catalog.DataBind();

        ListItem li = new ListItem();
        li.Text = "全部";
        li.Value = "0";
        ddl_xeke_Catalog.Items.Insert(0, li);
    }

    //绑定学年
    private void BinderXueNian()
    {
        ListItem li1 = new ListItem();
        li1.Text = "小学低年级";
        li1.Value = "1";
        ddl_xuenian.Items.Add(li1);
        ListItem li2 = new ListItem();
        li2.Text = "小学高年级";
        li2.Value = "2";
        ddl_xuenian.Items.Add(li2);
        ListItem li3 = new ListItem();
        li3.Text = "初一年级";
        li3.Value = "3";
        ddl_xuenian.Items.Add(li3);
        ListItem li4 = new ListItem();
        li4.Text = "初二年级";
        li4.Value = "4";
        ddl_xuenian.Items.Add(li4);
        ListItem li5 = new ListItem();
        li5.Text = "初三年级";
        li5.Value = "5";
        ddl_xuenian.Items.Add(li5);
        ListItem li6 = new ListItem();
        li6.Text = "高一年级";
        li6.Value = "6";
        ddl_xuenian.Items.Add(li6);
        ListItem li7 = new ListItem();
        li7.Text = "高二年级";
        li7.Value = "7";
        ddl_xuenian.Items.Add(li7);
        ListItem li8 = new ListItem();
        li8.Text = "高三年级";
        li8.Value = "8";
        ddl_xuenian.Items.Add(li8);
        ListItem li = new ListItem();
        li.Text = "全部";
        li.Value = "0";
        ddl_xuenian.Items.Insert(0, li);
    }
    //绑定职务
    private void BinderJob()
    {
        Job obj = new Job();
        string search = "";
        DataTable dt = obj.Query(search);
        ddl_zhiwu.DataSource = dt;
        ddl_zhiwu.DataTextField = "TeajobName";
        ddl_zhiwu.DataValueField = "TeajobId";
        ddl_zhiwu.DataBind();

        ListItem li = new ListItem();
        li.Text = "全部";
        li.Value = "0";
        ddl_zhiwu.Items.Insert(0, li);
    }
    //绑定职称
    private void BinderDuty()
    {
        Duty obj = new Duty();
        string search = "";
        DataTable dt = obj.Query(search);
        ddl_zhicheng.DataSource = dt;
        ddl_zhicheng.DataTextField = "TeadutyName";
        ddl_zhicheng.DataValueField = "TeadutyId";
        ddl_zhicheng.DataBind();

        ListItem li = new ListItem();
        li.Text = "全部";
        li.Value = "0";
        ddl_zhicheng.Items.Insert(0, li);
    }
    //绑定当前学历
    private void BinderXueLi()
    {
        ListItem li1 = new ListItem();
        li1.Text = "中专";
        li1.Value = "1";
        ddl_xueli.Items.Add(li1);
        ListItem li2 = new ListItem();
        li2.Text = "大专";
        li2.Value = "2";
        ddl_xueli.Items.Add(li2);
        ListItem li3 = new ListItem();
        li3.Text = "本科";
        li3.Value = "3";
        ddl_xueli.Items.Add(li3);
        ListItem li4 = new ListItem();
        li4.Text = "硕士";
        li4.Value = "4";
        ddl_xueli.Items.Add(li4);
        ListItem li5 = new ListItem();
        li5.Text = "博士";
        li5.Value = "5";
        ddl_xueli.Items.Add(li5);
        ListItem li6 = new ListItem();
        li6.Text = "高中及以下";
        li6.Value = "6";
        ddl_xueli.Items.Add(li6);
        ListItem li = new ListItem();
        li.Text = "全部";
        li.Value = "0";
        ddl_xueli.Items.Insert(0, li);
    }
    //绑定特殊分类
    private void BinderSepcial()
    {
        ListItem li1 = new ListItem();
        li1.Text = "班主任";
        li1.Value = "1";
        ddl_Sepcial.Items.Add(li1);
        ListItem li2 = new ListItem();
        li2.Text = "外语教师";
        li2.Value = "2";
        ddl_Sepcial.Items.Add(li2);
        ListItem li3 = new ListItem();
        li3.Text = "新上岗教师";
        li3.Value = "3";
        ddl_Sepcial.Items.Add(li3);
        ListItem li4 = new ListItem();
        li4.Text = "市骨干";
        li4.Value = "4";
        ddl_Sepcial.Items.Add(li4);
        ListItem li5 = new ListItem();
        li5.Text = "一般教师";
        li5.Value = "5";
        ddl_Sepcial.Items.Add(li5);
        ListItem li6 = new ListItem();
        li6.Text = "特级教师";
        li6.Value = "6";
        ddl_Sepcial.Items.Add(li6);
        ListItem li = new ListItem();
        li.Text = "全部";
        li.Value = "0";
        ddl_Sepcial.Items.Insert(0, li);
    }


    protected override void InitPage()
    {
        btn_Add.Text = GetLanageValueByKey("btn_Save", PowerCreator.LMS.Language.LanagetItemType.Page);
        btn_Search.Text = GetLanageValueByKey("btn_Search", PowerCreator.LMS.Language.LanagetItemType.Page);
        btn_SortSearch.Text = GetLanageValueByKey("btn_Search", PowerCreator.LMS.Language.LanagetItemType.Page);
        chkAll.Text = GetLanageValueByKey("RoleUser_rdbtn_YesNoAll", PowerCreator.LMS.Language.LanagetItemType.Page);
        chkCancel.Text = GetLanageValueByKey("SetBaseAction_rdbtn_All", PowerCreator.LMS.Language.LanagetItemType.Page);
        base.InitPage();
    }


    private string GetSearch()
    {
        StringBuilder sbSearch = new StringBuilder();
        if (ViewState["CID"] != null)
        {
            sbSearch.Append(string.Format(" SELECT distinct Learner_Id, TeacherId, UserName, UserPIC, Name, Sex, Email, Mobile, Education, Sepcial, XueNian, School,ZhiCheng ,ZhiWu,XueDuan, XueKe FROM TO_Learner where   Learner_Id not in (select UserID from TO_UserOfClass where ClassID={0})", ViewState["CID"].ToString()));
            if (ViewState["SearchType"] == null)
                ViewState["SearchType"] = "0";
            if (ViewState["SearchType"].ToString() == "1")
            {
                if (!string.IsNullOrEmpty(txt_UserName.Text))
                {
                    sbSearch.Append(string.Format(" and UserName like '%{0}%' ", txt_UserName.Text));
                }
                if (!string.IsNullOrEmpty(txt_RealName.Text))
                {
                    sbSearch.Append(string.Format(" and Name like '%{0}%' ", txt_RealName.Text));
                }

                if (ddl_School.SelectedValue.ToString() != "0")
                {
                    sbSearch.Append(string.Format(" and School = {0}  ", ddl_School.SelectedValue.ToString()));
                }
                if (ddl_region.SelectedValue.ToString() != "0")
                {
                    sbSearch.Append(string.Format(" and Region = {0}  ", ddl_region.SelectedValue.ToString()));
                }
                //if (ddl_UserGroup.SelectedValue.ToString() != "0")
                //{
                //    sbSearch.Append(string.Format(" and UserGroup = {0}  ", ddl_UserGroup.SelectedValue.ToString()));
                //}

                //if (ddl_UserGroup.SelectedValue.ToString() != "0")
                //{
                //    sbSearch.Append(string.Format(" and TO_Learner.UserGroup = {0}  ", ddl_UserGroup.SelectedValue.ToString()));
                //}
                if (!string.IsNullOrEmpty(txt_TeacherID.Text))
                {
                    sbSearch.Append(string.Format(" and TeacherId like '%{0}%' ", txt_TeacherID.Text));
                }

                if (ddl_xuenian.SelectedValue.ToString() != "0")
                {
                    sbSearch.Append(string.Format(" and XueNian = {0}  ", ddl_xuenian.SelectedValue.ToString()));
                }

                if (ddl_xeke_Catalog.SelectedValue.ToString() != "0")
                {
                    sbSearch.Append(string.Format(" and XueKe = {0}  ", ddl_xeke_Catalog.SelectedValue.ToString()));
                }
                if (ddl_xueduan_Stage.SelectedValue.ToString() != "0")
                {
                    sbSearch.Append(string.Format(" and XueDuan = {0}  ", ddl_xueduan_Stage.SelectedValue.ToString()));
                }
                if (ddl_zhiwu.SelectedValue.ToString() != "0")
                {
                    sbSearch.Append(string.Format(" and ZhiWu = {0}  ", ddl_zhiwu.SelectedValue.ToString()));
                }
                if (ddl_zhicheng.SelectedValue.ToString() != "0")
                {
                    sbSearch.Append(string.Format(" and ZhiCheng= {0}  ", ddl_zhicheng.SelectedValue.ToString()));
                }
                if (ddl_xueli.SelectedValue.ToString() != "0")
                {
                    sbSearch.Append(string.Format(" and Education = {0}  ", ddl_xueli.SelectedValue.ToString()));
                }
                if (ddl_Sepcial.SelectedValue.ToString() != "0")
                {
                    sbSearch.Append(string.Format(" and Sepcial = {0}  ", ddl_Sepcial.SelectedValue.ToString()));
                }

            }
            else if (ViewState["SearchType"].ToString() == "0")
            {
                sbSearch.Append(string.Format(" and ( TO_Learner.UserName like '%{0}%' or  TO_Learner.Name like '%{0}%')", txt_SearchKey.Text));
            }

            if (sbSearch.ToString().EndsWith("and "))
                sbSearch.Remove(sbSearch.ToString().LastIndexOf("and "), 4);

        }
        return sbSearch.ToString();
    }
    private void BinderPager(string search)
    {
        AspNetPager1.PageSize = thisPageSize;
        AspNetPager1.RecordCount = objView.Count(search);
    }

    private void BinderRPT(string search,int pageIndex)
    {
        DataTable dt = new DataTable();
        string orderlist = " Learner_Id desc";
        dt = objView.Query(search,orderlist, thisPageSize, pageIndex);
        dt.Columns.Add("StrEducation");
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            switch (dt.Rows[i]["Education"].ToString())
            {
                case "1":
                    dt.Rows[i]["StrEducation"] = "中专";
                    break;
                case "2":
                    dt.Rows[i]["StrEducation"] = "大专";
                    break;
                case "3":
                    dt.Rows[i]["StrEducation"] = "本科";
                    break;
                case "4":
                    dt.Rows[i]["StrEducation"] = "硕士";
                    break;
                case "5":
                    dt.Rows[i]["StrEducation"] = "博士";
                    break;
                case "6":
                    dt.Rows[i]["StrEducation"] = "高中及以下";
                    break;
                default:
                    dt.Rows[i]["StrEducation"] = "——";
                    break;
            }
        }


        dt.Columns.Add("SchoolGroupName");
        dt.Columns.Add("TeadutyName");
        dt.Columns.Add("TeajobName");
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            try
            {
                voSchool = objSchool.Load(int.Parse(dt.Rows[i]["School"].ToString()));
                dt.Rows[i]["SchoolGroupName"] = voSchool.SchoolGroupName;
            }
            catch (Exception ex)
            {
                dt.Rows[i]["SchoolGroupName"] ="--";
            }
            try
            {
                voDuty = objDuty.Load(int.Parse(dt.Rows[i]["ZhiCheng"].ToString()));
                dt.Rows[i]["TeadutyName"] = voDuty.TeadutyName;
            }
            catch (Exception ex)
            {
                dt.Rows[i]["TeadutyName"] = "--";
            }
            try
            {
                voJob = objJob.Load(int.Parse(dt.Rows[i]["ZhiWu"].ToString()));
                dt.Rows[i]["TeajobName"] = voJob.TeajobName;
            }
            catch (Exception ex)
            {
                dt.Rows[i]["TeajobName"] = "--";
            }

            
           
            
        }
        rpt_User.DataSource = dt;
        rpt_User.DataBind();
    }
    

    protected void btn_Search_Click(object sender, EventArgs e)
    {
        ViewState["SearchType"] = "1";
        string search = GetSearch();
        BinderPager(search);
        BinderRPT(search, 1);
    }
    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        string search = GetSearch();
        BinderRPT(search, AspNetPager1.CurrentPageIndex);
    }
    protected void btn_Add_Click(object sender, EventArgs e)
    {
        VOTOUserOfClass vo = new VOTOUserOfClass();
        TOUserOfClass obj = new TOUserOfClass();
        PowerCreator.LMS.Web.Base.BasePage objPage = new PowerCreator.LMS.Web.Base.BasePage();
        int userid = objPage.GetCreatedByInfo().UserID;
        if(ViewState["CID"]!=null)
        {
            for (int i = 0; i < rpt_User.Items.Count; i++)
            {
                CheckBox cbox = (CheckBox)rpt_User.Items[i].FindControl("chkSelect");
                if (cbox.Checked)
                {
                    Label lblid = (Label)rpt_User.Items[i].FindControl("lblid");
                    string sql = " ClassID=" + ViewState["CID"].ToString() + " and UserID=" + lblid.Text + "";
                    if (obj.Count(sql) == 0)
                    {
                        vo.ClassID = int.Parse(ViewState["CID"].ToString());
                        vo.UserID = int.Parse(lblid.Text);
                        vo.AddTime = DateTime.Now;
                        vo.Creator = userid;
                        obj.Add(vo);
                    }
                }
            }
        }
        string search = GetSearch();
        BinderPager(search);
        BinderRPT(search, AspNetPager1.CurrentPageIndex);
    }
    protected void btn_Back_Click(object sender, EventArgs e)
    {
        string url = "ManageClassUser.aspx?CID=" + ViewState["CID"].ToString();
        Script.RedirectURL(url);

    }

    protected void rpt_User_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName == "laddclassuser")
        {
            VOTOUserOfClass vo = new VOTOUserOfClass();
            TOUserOfClass obj = new TOUserOfClass();
            PowerCreator.LMS.Web.Base.BasePage objPage = new PowerCreator.LMS.Web.Base.BasePage();
            int userid = objPage.GetCreatedByInfo().UserID;
            if (ViewState["CID"] != null)
            {
                string sql = " ClassID=" + ViewState["CID"].ToString() + " and UserID=" + e.CommandArgument.ToString() + "";
                if (obj.Count(sql) == 0)
                {
                    vo.ClassID = int.Parse(ViewState["CID"].ToString());
                    vo.UserID = int.Parse(e.CommandArgument.ToString());
                    vo.AddTime = DateTime.Now;
                    vo.Creator = userid;
                }
                if (obj.Add(vo))
                {
                    string search = GetSearch();
                    BinderPager(search);
                    BinderRPT(search, AspNetPager1.CurrentPageIndex);
                }
            }
        }
    }
  

  
    protected void btn_SortSearch_Click(object sender, EventArgs e)
    {
        ViewState["SearchType"] = "0";
        string search = GetSearch();
        BinderPager(search);
        BinderRPT(search, AspNetPager1.CurrentPageIndex);
    }

    protected void chkAll_CheckedChanged(object sender, EventArgs e)
    {
        chkCancel.Checked = false;
        //全选 / 取消
        for (int i = 0; i < rpt_User.Items.Count; i++)
        {
            CheckBox cbox = (CheckBox)rpt_User.Items[i].FindControl("chkSelect");
            if (chkAll.Checked)
                cbox.Checked = true;
            else
                cbox.Checked = false;
        }
    }
    protected void chkCancel_CheckedChanged(object sender, EventArgs e)
    {
        chkAll.Checked = false;
        //反选
        for (int i = 0; i < rpt_User.Items.Count; i++)
        {
            CheckBox cbox = (CheckBox)rpt_User.Items[i].FindControl("chkSelect");
            if (cbox.Checked)
                cbox.Checked = false;
            else
                cbox.Checked = true;
        }
    }
}